package org.jsola.contract.web;

import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dto.ContractGroupAddDTO;
import org.jsola.contract.dto.ContractGroupUpdateDTO;
import org.jsola.contract.query.ContractGroupQuery;
import org.jsola.contract.service.IContractGroupService;
import org.jsola.contract.vo.ContractGroupListVO;
import org.jsola.contract.vo.ContractGroupVO;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 合同类型分组
 *
 * @author JanusMix
 */
@RestController("contractContractGroupController")
@RequestMapping(ContractConstants.API_PATH + "/v1/contractGroups")
public class ContractGroupController implements IContractGroupControllerAPI {

    @Autowired
    private IContractGroupService contractGroupService;

    @Override
    @PostMapping
    public Result<ContractGroupVO> save(@Valid @RequestBody ContractGroupAddDTO contractGroupAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增合同类型分组权限校验

        return Result.success(
                contractGroupService.save(contractGroupAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody ContractGroupUpdateDTO contractGroupUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改合同类型分组权限校验

        int count = contractGroupService.update(contractGroupUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{contractGroupId}")
    public Result<Integer> delete(@PathVariable Long contractGroupId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除合同类型分组权限校验

        int count = contractGroupService.deleteByIds(tokenUser, contractGroupId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] contractGroupIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除合同类型分组权限校验

        int count = contractGroupService.deleteByIds(tokenUser, contractGroupIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{contractGroupId}")
    public Result<ContractGroupVO> findById(@PathVariable Long contractGroupId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同类型分组权限校验

        return Result.success(
                contractGroupService.selectById(contractGroupId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<ContractGroupListVO>> listAll(@Valid ContractGroupQuery contractGroupQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同类型分组权限校验

        return Result.success(
                contractGroupService.select(contractGroupQuery, tokenUser)
        );
    }


    @Override
    @GetMapping
    public Result<Page<ContractGroupListVO>> page(@Valid ContractGroupQuery contractGroupQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                contractGroupService.selectPage(contractGroupQuery, tokenUser.getSiteId())
        );
    }


}

